<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        h1{
            font-size: 72px;
            color: #0096dc;
            margin-bottom: 0;
        }
    </style>
</head>
<body>
<div>
    <h1>烘焙首页</h1>
</div>
<div id="app">
    <el-upload
            action="/picture/fileUpload"
            name="file"
            limit="9"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <div>
        <input type="text" placeholder="图片简介..." v-model="picture.description">
        <input type="button" value="添加图片" @click="insert()">
    </div>
    <div>
        <input type="button" va.3
               lue="查询图片" @click="select()">
    </div>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <el-row gutter="20">
        <el-col span="6" v-for="p in pictureArr">
            <el-card>
                <a>
                    <img :src="p.url" width="100%" height="233">
                    <p>{{p.description}}</p>
                </a>
            </el-card>
            <el-button @click="deletePicture(p.id)">删除</el-button>
            <el-button @click="updatePicture(p.id)">修改</el-button>
        </el-col>
    </el-row>




</div>
</body>
<!--引入axios和vue框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                picture:{description:"",url:""},
                fileList:[],
                pictureArr:[]
            }
        },
        methods:{
            insert(){
                //得到多张图片路径 合并成一个字符串 赋值给urls
                if (v.fileList.length==0){
                    alert("请选择上传的图片!");
                    return;
                }
                let arr = [];
                for (let file of v.fileList) {
                    arr.push(file.response);
                }
                v.picture.url=arr.toString();
                console.log(v.picture)
                //发出发微博的请求
                axios.post("/picture/insert",v.picture).then(function (response) {
                    alert("上传成功")
                    location.href="/";//回到首页
                })

            },
            select(){
                axios.post("/picture/select").then(function (response) {
                    console.log("response.data===",response.data)
                    v.pictureArr = response.data;
                })
            },
            deletePicture(id){
                axios.post("/picture/delete/"+id).then(function (response) {
                    alert("删除成功")
                    location.href="/"
                })
            },
            updatePicture(id){
                axios.post("/picture/update/"+id).then(function (response) {
                    alert("修改成功")
                })
            },
            handleSuccess(response,file,fileList){
                v.fileList = fileList;
            },
            handleRemove(file, fileList) {
                v.fileList = fileList;
                console.log(file, fileList);
                //发出删除文件的请求
                axios.get("/remove?url="+file.response).then(function (response) {
                    console.log("服务器文件删除完成!");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        }
    })
</script>
</html>